<?php 
//安装前开启钩子APP_INIT
class IndexController extends Controller{
	// 构造函数
	public function __init()
	{
		//有锁文件不能执行安装
		if (is_file(MODULE_PATH.'lock.php')) {
			$this->display();
		}
	}

	//版权声明
	public function install()
	{
		$this->display();
	}

	//设置数据库
	public function setDB()
	{
		$this->display();
	}

	//安装数据
	public function insertSql()
	{
		$status=mysql_connect($_POST['DB_HOST'],$_POST['DB_USER'],$_POST['DB_PASSWORD']);
		if (!$status) {
			$this->error('主机连接失败','setDB');
		}
		if(!mysql_select_db($_POST['DB_DATABASE'])){
			$this->error('数据库不存在','setDB');
		}
		//更新C函数记录
		C($_POST);
		$db=M();///
		$db_prefix='hd_';
		//先导入数据库结构
		require MODULE_PATH.'Sql/structure.php';
		//获得数据库文件
		$file=Dir::tree(MODULE_PATH."Sql");
		foreach ($file as $v) {
			if (substr($v['filename'],0,3)=='hd_') {
				require $v['path'];//
			}
		}
		//更新管理员信息
		$code=md5(mt_rand(1,1000).time());
		$data['uid']=1;
		$data['username']=$_POST['username'];
		$data['password']=md5($_POST['password'].$code);
		$data['code']=$code;
		$db->table('admin')->save($data);
		//更新配置
		$inc="<?php \n return ".var_export($_POST,true).";\n?>";
		file_put_contents(APP_CONFIG_PATH.'db.inc.php', $inc);

		go('finish');

	}
	//安装结束
	public function finish()
	{
		$this->display();
		//创建锁文件
		touch(MODULE_PATH.'lock.php');
	}


}


 ?>